package design_pattern.ObserveThread.plus;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/30 15:18
 * @Version 1.0
 */
public abstract class ObservableRunnable implements Runnable{
    final protected LifeCycleListener listener;
    ObservableRunnable(final LifeCycleListener listener){
        //通过构造函数 把listener 加到 ObservableRunnable 里面去。
        this.listener = listener;
    }

    protected void notifyChange(final RunnableEvent event){
        listener.onEvent(event);
    }

    public enum RunnableState{
        RUNNING,ERROR,DONE
    }

    public static class RunnableEvent{
        private final RunnableState state;
        private final Thread thread;
        private final Throwable cause;

        public RunnableEvent(RunnableState state, Thread thread, Throwable cause) {
            this.state = state;
            this.thread = thread;
            //这里的Throwable 就是上面抛出的东西，这里是异常。
            this.cause = cause;
        }

        public RunnableState getState() {
            return state;
        }

        public Thread getThread() {
            return thread;
        }

        public Throwable getCause() {
            return cause;
        }
    }

}
